package cn.virens.web.components.beetl;

import java.util.Collections;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

import org.beetl.core.Format;
import org.beetl.core.Function;
import org.beetl.core.GroupTemplate;
import org.beetl.core.TagFactory;
import org.beetl.ext.spring.BeetlGroupUtilConfiguration;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import cn.virens.components.spring.ApplicationContextUtil;

public class BeetlGroupUtilConfigurationBean extends BeetlGroupUtilConfiguration implements InitializingBean, ApplicationContextAware {
	protected GroupTemplate groupTemplate;
	protected String root = "/";
	protected String webPath = null;

	// 自定义函数
	private Map<String, Function> functions = Collections.emptyMap();
	// 自定义格式化器
	private Map<String, Format> formats = Collections.emptyMap();
	// 自定义标签
	private Map<String, TagFactory> tagFactorys = Collections.emptyMap();

	/**
	 * 配置属性
	 */
	protected Properties configProperties = null;

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		ApplicationContextUtil.inject(tagFactorys, applicationContext);// 判断自定义标签是否实现ApplicationContextAware 接口，如果实现了就调用
		ApplicationContextUtil.inject(functions, applicationContext);// 判断自定义函数是否实现ApplicationContextAware 接口，如果实现了就调用
		ApplicationContextUtil.inject(formats, applicationContext);// 判断自定义格式化是否实现ApplicationContextAware 接口，如果实现了就调用
	}

	@Override
	public void afterPropertiesSet() {
		super.setConfigProperties(configProperties);
		super.init();
	}

	@Override
	public void config(GroupTemplate groupTemplate) {
		super.config(groupTemplate);

		// 注册自定义函数
		for (Entry<String, Function> entry : functions.entrySet()) {
			groupTemplate.registerFunction(entry.getKey(), entry.getValue());
		}
		// 注册自定义格式化器
		for (Entry<String, Format> entry : formats.entrySet()) {
			groupTemplate.registerFormat(entry.getKey(), entry.getValue());
		}
		// 注册自定义标签
		for (Entry<String, TagFactory> entry : tagFactorys.entrySet()) {
			groupTemplate.registerTagFactory(entry.getKey(), entry.getValue());
		}
	}

	public Properties getConfigProperties() {
		return configProperties;
	}

	public void setConfigProperties(Properties configProperties) {
		this.configProperties = configProperties;
	}

	public void putConfigProperties(Object key, Object value) {
		if (configProperties == null) {
			this.configProperties = new Properties();
		}

		this.configProperties.put(key, value);
	}

	/**
	 * 自定义函数
	 *
	 * @param functions
	 */
	public void setFunctions(Map<String, Function> functions) {
		this.functions = functions;
	}

	/**
	 * 自定义格式化器
	 *
	 * @param formats
	 */
	public void setFormats(Map<String, Format> formats) {
		this.formats = formats;
	}

	/**
	 * 自定义标签
	 *
	 * @param tagFactorys
	 */
	public void setTagFactorys(Map<String, TagFactory> tagFactorys) {
		this.tagFactorys = tagFactorys;
	}

	/**
	 * 注册 自定义函数
	 * 
	 * @param name
	 * @param function
	 */
	public void registerFunction(String name, Function function) {
		this.functions.put(name, function);
	}

	/**
	 * 注册 自定义格式化器
	 * 
	 * @param name
	 * @param format
	 */
	public void registerFormat(String name, Format format) {
		this.formats.put(name, format);
	}

	/**
	 * 注册 自定义标签
	 * 
	 * @param name
	 * @param tagFactory
	 */
	public void registerTagFactory(String name, TagFactory tagFactory) {
		this.tagFactorys.put(name, tagFactory);
	}
}
